<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 1.xxx公司 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon
              icon-class="bank"
            /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--2.树状解构 -->
        <el-tree :data="list" default-expand-all>
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon v-if="!scope.data.children.length" icon-class="user" />
                <svg-icon v-else :icon-class="scope.node.expanded ? 'jian' : 'jia'" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- dialog弹出框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      :title="isEdit?'编辑部门':'新增部门'"
      @close="hCloseReset"
    >
      <depDialog
        ref="refDepDialog"
        :pid="pid"
        :origin-list="originList"
        :is-edit="isEdit"
        @hClose="hClose"
        @reset="reset"
      />
    </el-dialog>
  </div>
</template>
<script>
import { loadDepartment, delDept } from '@/api/department'
import { array2Tree } from '@/utils/index'
import depDialog from './depDialog'
export default {
  components: { depDialog },
  // 1.数据
  data() {
    return {
      // 1.1部门数据
      list: [],
      // 1.2 dialog的弹框布尔值
      showDialog: false,
      // 1.3 获取点击的id
      pid: '',
      // 1.4是编辑还是添加
      isEdit: false,
      // 1.5传递给子组件校验表单
      originList: []
    }
  },
  // 2.created
  created() {
    // 2.1调用获取部门列表
    this.getDepartment()
  },
  // 3.方法
  methods: {
    // 3.1获取部门列表
    async getDepartment() {
      // (1)调用api中的请求
      const res = await loadDepartment()
      // (2)去除数组第一个元素,shift会改变原数组
      res.data.depts.shift()
      // (3)将数据保存在data中
      this.list = array2Tree(res.data.depts, '')
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          code: item.code,
          name: item.name
        }
      })
    },
    // 3.2添加按钮
    hAdd(id) {
      // (1)显示弹出框
      this.showDialog = true
      this.pid = id
      // (2)告诉子组件是添加按钮
      this.isEdit = false
    },
    // 3.3 子组件添加成功,
    reset(msg) {
      this.$message.success(msg)
      this.showDialog = false
      this.getDepartment()
    },
    // 3.4删除部门
    hDel(id) {
      this.$confirm('请问您确定要删除吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // (1)发请求
        await delDept(id)
        // (2)弹窗+刷新数据
        this.$message.success('删除成功')
        this.getDepartment()
      }).catch(() => {
      })
    },
    // 3.5编辑部门
    hEdit(id) {
      // (1)将id传给pid
      this.pid = id
      // (2)告诉子组件是编辑按钮
      this.isEdit = true
      this.showDialog = true
      // (3)调用子组件的方法,获取当前点击编辑的信息
      this.$nextTick(() => {
        this.$refs.refDepDialog.deptInfo()
      })
    },
    // 3.6子组件通知关闭弹窗
    hClose() {
      this.showDialog = false
    },
    // 3.7表单关闭触发
    hCloseReset() {
      this.$refs.refDepDialog.formReset()
    }
  }
}
</script>
